<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Vico的个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="const 与 constexpr 的区别 在C语言中 const 表示只读，readonly    但是在C++中产生了二义性   还给赋予了常量的的意思 C++11 中提出了 constexpr  表示常量 （可以在编译时期检查是不是常量） 但是const 只能在运行时 表示检查 最常见的 数组定义是需要 使用产量关键词的 const int len  &#x3D; 10;   &#x2F;&#x2F;也可是  const">
<meta property="og:type" content="article">
<meta property="og:title" content="Vico的个人博客">
<meta property="og:url" content="https://vicolj.gitee.io/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/index.html">
<meta property="og:site_name" content="Vico的个人博客">
<meta property="og:description" content="const 与 constexpr 的区别 在C语言中 const 表示只读，readonly    但是在C++中产生了二义性   还给赋予了常量的的意思 C++11 中提出了 constexpr  表示常量 （可以在编译时期检查是不是常量） 但是const 只能在运行时 表示检查 最常见的 数组定义是需要 使用产量关键词的 const int len  &#x3D; 10;   &#x2F;&#x2F;也可是  const">
<meta property="og:locale">
<meta property="article:published_time" content="2021-03-13T03:37:28.310Z">
<meta property="article:modified_time" content="2021-03-13T04:38:15.228Z">
<meta property="article:author" content="Vico">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Vico的个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Vico的个人博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">Vico的个人博客</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://vicolj.gitee.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-const 与 constexpr 的区别" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/" class="article-date">
  <time class="dt-published" datetime="2021-03-13T03:37:28.310Z" itemprop="datePublished">2021-03-13</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>const 与 constexpr 的区别</p>
<p>在C语言中 const 表示只读，readonly   </p>
<p>但是在C++中产生了二义性   还给赋予了常量的的意思</p>
<p>C++11 中提出了 constexpr  表示常量 （可以在编译时期检查是不是常量）</p>
<p>但是const 只能在运行时 表示检查</p>
<p>最常见的 数组定义是需要 使用产量关键词的</p>
<p>const int len  = 10;   //也可是  constexpr  int len  = 10;</p>
<p>char str[len];//is ok</p>
<p>int len  = 10;</p>
<p>char str[len];//is no ok</p>
<p>可以放在 构造函数中初始化 ！！！ 非常的方便</p>
<p>.h</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">class MagicFoo&#123;</span><br><span class="line">public:</span><br><span class="line">	std::vector&lt;int&gt; vec;</span><br><span class="line">  	MagicFoo(std::initializer_list&lt;int&gt; list) &#123;</span><br><span class="line">	&#x2F;&#x2F; 从 C++11 起, 使用 auto 关键字进行类型推导</span><br><span class="line">	for (auto it &#x3D; list.begin(); it !&#x3D; list.end(); ++it) &#123;   &#x2F;&#x2F;这个里面就不需要一个一个赋值了</span><br><span class="line">		vec.push_back(*it);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;  </span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>.cpp</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">int main() &#123;</span><br><span class="line">    MagicFoo magicFoo &#x3D; &#123;1, 2, 3, 4, 5&#125;;</span><br><span class="line">    std::cout &lt;&lt; &quot;magicFoo: &quot;;</span><br><span class="line">    for (auto it &#x3D; magicFoo.vec.begin(); it !&#x3D; magicFoo.vec.end(); ++it) &#123;</span><br><span class="line">    	std::cout &lt;&lt; *it &lt;&lt; &quot;, &quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	std::cout &lt;&lt; std::endl;</span><br><span class="line">	return 0;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>



      
    </div>
    <footer class="article-footer">
      <a data-url="https://vicolj.gitee.io/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/" data-id="ckmup9nxi000034d542wddt1x" data-title="" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/29/IEC104%E8%A7%84%E8%8C%83/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          IEC104规范
        
      </div>
    </a>
  
  
    <a href="/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">QT 日志框架系统</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/IEC%E8%A7%84%E8%8C%83/" rel="tag">IEC规范</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/QT/" rel="tag">QT</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/IEC%E8%A7%84%E8%8C%83/" style="font-size: 10px;">IEC规范</a> <a href="/tags/QT/" style="font-size: 20px;">QT</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/03/29/IEC104%E8%A7%84%E8%8C%83/">IEC104规范</a>
          </li>
        
          <li>
            <a href="/2021/03/13/const%20%E4%B8%8E%20constexpr%20%E7%9A%84%E5%8C%BA%E5%88%AB/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/03/08/QT%20%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6%E7%B3%BB%E7%BB%9F/">QT 日志框架系统</a>
          </li>
        
          <li>
            <a href="/2021/03/08/QT-%E7%BF%BB%E8%AF%91-%E6%9C%89%E5%85%B3tr%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B/">QT 翻译 有关tr的那些事</a>
          </li>
        
          <li>
            <a href="/2021/03/08/%E4%BD%BF%E7%94%A8%20QVariant%20%E4%BC%A0%E9%80%92%E6%8C%87%E9%92%88%E7%9A%84%E5%A6%99%E7%94%A8/">使用 QVariant 传递指针   的妙用</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Vico<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>